﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using YouHP.Model;
using YouHP.DALFactory;
using YouHP.Utility;

namespace YouHP.BLL
{
    public class PatientPrescriptionBLL
    {
        #region 单件模式
        static PatientPrescriptionBLL _Instance;

        public static PatientPrescriptionBLL Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new PatientPrescriptionBLL();
                }
                return _Instance;
            }
        }
        #endregion

        public List<PrescriptionHistoryInfo> GetAllCurUsingPrescriptions(int prId)
        {
            return DataAccess.PatientPrescriptionDAL.GetUsingPrescriptionList(prId);
        }

        public PrescriptionHistoryInfo GetCurrentPrescription(int prId, int presId)
        {
            return DataAccess.PatientPrescriptionDAL.GetCurrentPrescription(prId, presId);
        }

        public List<PrescriptionHistoryInfo> GetAllStoppedPrescriptionList(int prId)
        {
            return DataAccess.PatientPrescriptionDAL.GetStoppedPrescriptionList(prId);
        }

        public bool IsCurUsingPrescription(int prId, int prescriptionId)
        {
            PrescriptionHistoryInfo info = DataAccess.PatientPrescriptionDAL.GetCurrentPrescription(prId, prescriptionId);
            return info.Id > 0;
        }

        public bool AddPrescriptionHistory(PrescriptionHistoryInfo info)
        {
            return DataAccess.PatientPrescriptionDAL.AddPrescriptionHistory(info);
        }

        public bool UpdatePrescriptionHistory(PrescriptionHistoryInfo info)
        {
            return DataAccess.PatientPrescriptionDAL.UpdatePrescriptionHistotry(info);
        }

        public bool StopPrescriptionHistory(int prId, int prescriptionId, DateTime date)
        {
            return DataAccess.PatientPrescriptionDAL.StopPrescriptionHistory(prId, prescriptionId, date);
        }

        public List<PrescriptionHistoryInfo> GetPrescriptionHistoryList(int prId, int prescriptionId)
        {
            return DataAccess.PatientPrescriptionDAL.GetPrescriptionHistoryList(prId, prescriptionId);
        }

        public List<PrescriptionHistoryInfo> GetPrescripitonHistoryListByPid(int prescriptionId)
        {
            return DataAccess.PatientPrescriptionDAL.GetPrescripitonHistoryListByPid(prescriptionId);
        }

        public List<PrescriptionHistoryInfo> GetPrescriptionHistoryList(int pageSize, int pageIndex, int prId, int prescriptionId, out int count)
        {
            return DataAccess.PatientPrescriptionDAL.GetPrescriptionHistoryList(pageSize, pageIndex, prId, prescriptionId, out count);
        }

        public List<int> GetIdsOfUserWhoDosePrescription(int pid)
        {
            return DataAccess.PatientPrescriptionDAL.GetIdsOfUserWhoDosePrescription(pid);
        }


        // Utility class, not exposed!
        class HistoryTimeSpan
        {
            public HistoryTimeSpan(DateTime start, DateTime end)
            {
                this.StartDate = start;
                this.EndDate = end;
                this.IsAllSet = true;
            }
            public HistoryTimeSpan(DateTime start)
            {
                this.StartDate = start;
                this.EndDate = DateTime.MaxValue;
                this.IsAllSet = false;
            }
            public void SetEndDate(DateTime end)
            {
                this.EndDate = end;
                this.IsAllSet = true;
            }

            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
            public bool IsAllSet { get; set; }
        }


        private List<HistoryTimeSpan> GetHistoryTimeSpans(int prId, int prescriptionId)
        {
            List<HistoryTimeSpan> spans = new List<HistoryTimeSpan>();

            List<PrescriptionHistoryInfo> histories = GetPrescriptionHistoryList(prId, prescriptionId);
            histories.Sort(new PrescriptionHistoryInfoComparer());
            foreach (PrescriptionHistoryInfo info in histories)
            {
                if (info.State == Constants.START_USE)
                {
                    HistoryTimeSpan span = new HistoryTimeSpan(info.StartDate);
                    spans.Add(span);
                }
                else if (info.State == Constants.STOPED_USE)
                {
                    spans[spans.Count - 1].SetEndDate(info.StartDate);
                }
            }

            return spans;
        }

        public bool CheckPrescriptionHistory(int prId, int prescriptionId, DateTime date)
        {
            List<HistoryTimeSpan> spans = GetHistoryTimeSpans(prId, prescriptionId);
            foreach (HistoryTimeSpan span in spans)
            {
                if (span.IsAllSet)
                {
                    if (DateTime.Compare(date, span.EndDate) < 0)
                        return true;

                    //if (DateTime.Compare(date, span.StartDate) > 0 && DateTime.Compare(date, span.EndDate) < 0)
                    //    return true;
                }
                else
                {
                    //if (DateTime.Compare(date, span.StartDate) < 0)
                        return true;
                }
            }
            return false;
        }
    }
}